package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.service.WebSocketServer;
import com.example.util.ResponseUtils;
import com.example.util.SystemInfoUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/7/27 14:07
 */
@Controller
public class WebSocketController {
    @Resource
    private WebSocketServer webSocketServer;

    @GetMapping("/websocket")
    public String websocket() {
        return "WebSocket";
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void sendMsg() {
//        webSocketServer.sendInfo("20", "hello");
        Map<String, Object> map = new HashMap<>(3);
        map.put("cpu", SystemInfoUtils.getCpu());
        map.put("mem", SystemInfoUtils.getMemory());
        map.put("disk", SystemInfoUtils.getDisk());
        webSocketServer.sendInfo(ResponseUtils.ok(map));
    }

}
